<?php
class send_email extends MX_Controller{
	public function index($from_email,$from_name,$to_email,$subject,$message){
		$this->load->library('email');
		
		$this->email->from($from_email, $from_name);
		$this->email->to($to_email);
		$this->email->subject($subject);
		$this->email->message($message);
		
		$this->email->send();
		//echo $this->email->print_debugger();
	}
	/*
	 * status:{send, wait, valide}
	 */
	public function add($from_email,$from_name,$to_email,$subject='',$message='',$status='',$action='',$description='',$send_direct=1){
		$email=new stdClass;
		$email->from_email=$from_email;
		$email->to_email=$to_email;
		$email->from_name=$from_name;
		$email->subject=$subject;
		$email->message=$message;
		if($email->subject==''){
			$extractLine="\n";
			$extract=explode($extractLine,$message);
			$email->subject=$extract[0];
			unset($extract[0]);
			$email->message=implode($extractLine,$extract);
		}
		$email->status=$status;
		$email->action=$action;
		$email->description=$description;
		$this->model()->add($email);
		if($send_direct)
			self::index($email->from_email,$email->from_name,$email->to_email,$email->subject,$email->message);
	}
}